package com.jetpackframework;

import android.content.Context;
import android.content.res.Configuration;

import com.gwm.annotation.layout.IOCWork;
import com.jetpackframework.applicationdelegate.ApplicationCall;
import com.jetpackframework.applicationdelegate.ApplicationDelegate;
import com.jetpackframework.applicationdelegate.BaseApplicationDelegate;
import com.jetpackframework.routerview.AppRouterViewLoader;
import com.jetpackframework.routerview.RouterViewLoader;
import com.jetpackframework.routerview.PathRouterViewLoader;
import com.jetpackframework.routerview.SystemRouterViewLoader;

import java.util.Iterator;
import java.util.ServiceLoader;

public class MergeApplicationDelegate extends BaseApplicationDelegate {

    private SparseArray<BaseApplicationDelegate> delegates;
    private RouterViewLoader routerViewLoader;

    @Override
    public void init(Application application) {
        super.init(application);
        delegates = new SparseArray<>();
        ServiceLoader<com.jetpackframework.applicationdelegate.ApplicationDelegate> loader = ServiceLoader.load(com.jetpackframework.applicationdelegate.ApplicationDelegate.class);
        Iterator<com.jetpackframework.applicationdelegate.ApplicationDelegate> iterator = loader.iterator();
        for (;iterator.hasNext();){
            delegates.put((BaseApplicationDelegate) iterator.next());
        }
        RouterViewLoader systemRouterViewLoader = new SystemRouterViewLoader();
        RouterViewLoader routerViewLoader = new PathRouterViewLoader(Application.PROJECT_NAME,systemRouterViewLoader);
        for (BaseApplicationDelegate delegate : delegates){
            delegate.init(application);
            if (delegate.getClass().isAnnotationPresent(IOCWork.class)){
                IOCWork iocWork = delegate.getClass().getAnnotation(IOCWork.class);
                if (iocWork != null && iocWork.value() != null) {
                    if (this.routerViewLoader == null){
                        this.routerViewLoader = new AppRouterViewLoader(iocWork.value(), routerViewLoader);
                    }else {
                        this.routerViewLoader = new AppRouterViewLoader(iocWork.value(),this.routerViewLoader);
                    }
                }else {
                    if (this.routerViewLoader == null){
                        this.routerViewLoader = routerViewLoader;
                    }
                }
            }
        }
    }
    @Override
    public void onCreate() {
        for (ApplicationDelegate delegate : delegates){
            ApplicationCall.callOnCreate(delegate.getHandler());
        }
    }

    @Override
    public void onLowMemory() {
        for (ApplicationDelegate delegate : delegates){
            ApplicationCall.callOnLowMemory(delegate.getHandler());
        }
    }

    @Override
    public void onTrimMemory(int level) {
        for (ApplicationDelegate delegate : delegates){
            ApplicationCall.callOnTrimMemory(delegate.getHandler(),level);
        }
    }

    @Override
    public void onTerminate() {
        for (ApplicationDelegate delegate : delegates){
            ApplicationCall.callOnTerminate(delegate.getHandler());
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        for (ApplicationDelegate delegate : delegates){
            ApplicationCall.callOnConfigurationChanged(delegate.getHandler(),newConfig);
        }
    }

    @Override
    public void onBaseContextAttached(Context base) {
        for (ApplicationDelegate delegate : delegates){
            ApplicationCall.callOnBaseContextAttached(delegate.getHandler(),base);
        }
    }

    @Override
    public void exit() {
        for (ApplicationDelegate delegate : delegates){
            ApplicationCall.callOnExit(delegate.getHandler());
        }
    }

    public RouterViewLoader getRouterViewLoader() {
        return routerViewLoader;
    }
}
